/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Practica5.practica;

import Practica5.structures.QueueException;
import Practica5.structures.StackException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author miguel
 */
public class BusquedaYRecorridos extends javax.swing.JFrame {

    /**
     * Creates new form BusquedaYRecorridos
     */
    public BusquedaYRecorridos() {
        initComponents();
        fileName = "";
        brg = new BusquedasYRecorridosGraficas();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ComboAlgoritmo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        Seleccion = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        vertexNum = new javax.swing.JTextField();
        ejecutar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        ComboAlgoritmo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "BFS", "DFS", "Topological-Sort" }));

        jLabel1.setText("Aplicar algoritmo de búsqueda:");

        Seleccion.setText("Seleccionar archivo");
        Seleccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SeleccionActionPerformed(evt);
            }
        });

        jLabel2.setText("Num Vertice a iniciar");

        vertexNum.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vertexNumActionPerformed(evt);
            }
        });

        ejecutar.setText("Ejecutar");
        ejecutar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ejecutarActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ComboAlgoritmo, 0, 524, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(vertexNum)
                        .addGap(91, 91, 91)
                        .addComponent(ejecutar))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Seleccion)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ComboAlgoritmo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(Seleccion)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ejecutar)
                    .addComponent(jLabel2)
                    .addComponent(vertexNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void vertexNumActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vertexNumActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_vertexNumActionPerformed

    private void SeleccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SeleccionActionPerformed
        // TODO add your handling code here:
        File f;
        f = new File(".");
        JFileChooser jfc = new JFileChooser();

        jfc.setCurrentDirectory(f);
        int returnVal = jfc.showOpenDialog(null);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            fileName = jfc.getSelectedFile().getAbsolutePath();
            this.jLabel3.setText(fileName);
        } else {
            JOptionPane.showMessageDialog(this, "Debes de escoger un archivo");
        }
    }//GEN-LAST:event_SeleccionActionPerformed

    private void ejecutarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ejecutarActionPerformed
        // TODO add your handling code here:
        if (!fileName.equals("")) {
            this.jTextArea1.setText("");
            int vertex = Integer.parseInt(this.vertexNum.getText());
            mg = new MyGraph(fileName);
            ArrayList a;
            if (ComboAlgoritmo.getSelectedItem().equals("BFS")) {
                try {
                    a = (ArrayList) brg.BFS(mg, vertex);
                    try (BufferedReader br = new BufferedReader(new FileReader("BFSoutput.txt"))) {
                        String res = null;
                        String res1 = "";
                        while ((res = br.readLine()) != null){
                            res1 += res+"\n";
                        }
                        this.jTextArea1.setText(res1);
                    }
                } catch (QueueException | IOException ex) {
                    Logger.getLogger(BusquedaYRecorridos.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (ComboAlgoritmo.getSelectedItem().equals("DFS")) {
                try {
                    a = (ArrayList) brg.DFS(mg, vertex);
                    try (BufferedReader br = new BufferedReader(new FileReader("DFSoutput.txt"))) {
                        String res = null;
                        String res1 = "";
                        while ((res = br.readLine()) != null){
                            res1 += res+"\n";
                        }
                        this.jTextArea1.setText(res1);
                    }
                } catch (StackException | IOException ex) {
                    Logger.getLogger(BusquedaYRecorridos.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else if (ComboAlgoritmo.getSelectedItem().equals("Topological-Sort")) {
                try {
                    a = (ArrayList) brg.TopologicalSort(mg, vertex);
                    try (BufferedReader br = new BufferedReader(new FileReader("Topological-SortOutput.txt"))) {
                        String res = null;
                        String res1 = "";
                        while ((res = br.readLine()) != null){
                            res1 += res+"\n";
                        }
                        this.jTextArea1.setText(res1);
                    }
                } catch (StackException | IOException ex) {
                    Logger.getLogger(BusquedaYRecorridos.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }//GEN-LAST:event_ejecutarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BusquedaYRecorridos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BusquedaYRecorridos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BusquedaYRecorridos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BusquedaYRecorridos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new BusquedaYRecorridos().setVisible(true);
            }
        });
    }
    //Declaración de variables del programa
    private String fileName;
    private MyGraph mg;
    private BusquedasYRecorridosGraficas brg;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ComboAlgoritmo;
    private javax.swing.JButton Seleccion;
    private javax.swing.JButton ejecutar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField vertexNum;
    // End of variables declaration//GEN-END:variables
}
